package com.tskj.leonsu.websocket;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.io.IOException;

/**
 * 扫描工具类，主要用于检查客户端，发送消息到客户端
 */
@Service
public class EasyStarSocketUtility {

    @Autowired
    private EasyStarProcess easyStarProcess;

    /**
     * 检查session和userId是否有效，
     *
     * @return true：有效\n
     * false：无效
     */
    public boolean exists(String token) {
        return easyStarProcess.exists(token);
    }

    public boolean exists(Session session) {
        return easyStarProcess.exists(session);
    }

//    public void sendMessage(Session session, int code, String errMsg) throws IOException, EncodeException {
//        JSONObject ret = JSONObject.parseObject("{}");
//        ret.put("code", code);
//        ret.put("errMsg", errMsg);
//        sendMessage(session, ret.toString());
//    }
//
//    public void sendMessage(Session session, int code, String errMsg, Object data) throws IOException, EncodeException {
//        JSONObject ret = JSONObject.parseObject("{}");
//        ret.put("code", code);
//        ret.put("errMsg", errMsg);
//        ret.put("data", data);
//        sendMessage(session, ret.toString());
//    }
//
//    public void sendMessage(Session session, String msg) throws IOException {
//        session.getBasicRemote().sendText(msg);
//    }

    private JSONObject createJson(int msgType, Object message) {
        JSONObject jsonSend = new JSONObject();
        switch (msgType) {
            case 0:
                jsonSend.put("data", message);
                break;
            default:
                jsonSend.put("errMsg", message);
        }
        jsonSend.put("code", msgType);
        return jsonSend;
    }

    public void sendMessage(String token, JSONObject message) throws IOException {
        easyStarProcess.sendMessage(token, message);
    }

    public void sendMessage(Session session, int msgType, Object message) throws IOException {
        session.getBasicRemote().sendText(createJson(msgType, message).toJSONString());
    }

    public void sendMessage(Session session, JSONObject message) throws IOException {
        session.getBasicRemote().sendText(message.toJSONString());
    }

    public void add(String token, Session session) {
        easyStarProcess.add(token, session);
    }

    public void update(String token, Session session) {
        easyStarProcess.update(token, session);
    }

    public void remove(String token) {
        easyStarProcess.remove(token);
    }

    public void remove(Session session) {
        easyStarProcess.remove(session);
    }

    public int size() {
        return easyStarProcess.size();
    }

    public String getToken(Session session) {
        return easyStarProcess.getToken(session);
    }

    public void sendToAll(int msgType, Object message) {
        easyStarProcess.sendToAll(createJson(msgType, message));
    }

    public void sendToAll(JSONObject obj) {
        easyStarProcess.sendToAll(obj);
    }
}
